<?php
namespace Crm\Model;

/**
 * 商品数据库模型
 *
 * @author SoChishun <14507247@qq.com>
 * @version 1.0
 * @since 1.0.0 <2015-3-27> SoChishun Added.
 */
class ProductModel extends AppbaseModel {

    /**
     * 数据表名称
     * @var string
     */
    protected $tableName = 'tproduct_goods';
    
    /**
     * 字段定义
     * @var array
     */
    protected $fields = array('id', 'product_name', 'barcode', 'model', 'type_id', 'category_id', 'price', 'market_price', 'cost_price', 'stock', 'warning_stock', 'sale_volume', 'discount_rate', 'discount_amount', 'discount_price', 'integral', 'visit_count', 'reply_count', 'thumb_url', 'picture_url', 'top_image', 'size', 'weight', 'unit_id', 'color_id', 'brand_id', 'grade_id', 'packaging_id', 'specification_id', 'orgin_id', 'manufacturer_id', 'description', 'meta_keywords', 'meta_description', 'start_time', 'end_time', 'owner_id', 'ex_tags', 'ordinal', 'state', 'create_time', 'update_time', 'site_id', '_pk' => 'id', '_autoinc' => true);
    
    /**
     * 命名范围
     * @var array
     */
    protected $_scope = array();
    
    /**
     * 是否批量验证
     * @var boolean
     */
    protected $patchValidate = true;
    
    /**
     * 验证规则
     * @var array
     */
    protected $_validate = array();
    
    /**
     * 自动填充
     * @var array
     */
    protected $_auto = array();
    
    /**
     * 保存商品
     * @return type
     * @since 1.0 <2015-3-31> SoChishun Added.
     */
    public function save_item(){
        $rule = array(
            array('product_name', 'require', '商品名称无效'),
            array('state', 'number', '状态无效'),
        );
        if(!$this->validate($rules)->create()){
            return $this->base_return_array(false,$this->getError());
        }
        // 上传文件
        $files = sofn_file_upload(array('skipEmpty' => true,'savePath'=>'Product/'));
        if (is_array($files)) {
            $this->thumb_url = $files['thumb_url']['filepath'];
        }
        if($this->data['id']){
            $result=$this->save();
        }else{
            $result=$this->add();
        }
        return $this->base_return_array($result);
    }
}